﻿
package com.unm.core;

import java.sql.ResultSet;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import org.json.JSONObject;

public class HandlerManager
{

    private static HashMap m_classes = new HashMap();
    private static HashMap m_action_handlers = new HashMap();

    public HandlerManager()
    {
    }

    private static void clear()
    {
        m_classes.clear();
        m_action_handlers.clear();
    }

    private static IHandler getClass(String class_name)
        throws Exception
    {
        if(m_classes.containsKey(class_name))
        {
            return (IHandler)m_classes.get(class_name);
        } else
        {
            IHandler cls = (IHandler)Class.forName(class_name).newInstance();
            m_classes.put(class_name, cls);
            return cls;
        }
    }

    public static IHandler getHandler(java.sql.Connection con, HttpServletRequest req)
        throws Exception
    {
    	clear();
    	String act = ((JSONObject)req.getAttribute("__cfg")).getString("ind");
    	String logic = req.getAttribute("__logic").toString();
        if(m_action_handlers.containsKey(act))
            return (IHandler)m_action_handlers.get(act);
        IHandler handler = getClass(logic.split(":")[0]); // com.base.login:loginout 的类
        req.setAttribute("__Func", logic.split(":")[1]);
	    m_action_handlers.put(act, handler);
	    return handler;
    }

}
